package com.kaiyidiannao.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.zxing.decoding.Intents;
import com.umeng.common.Log;

/**
 * Created by 大志 on 14-4-8.
 * 数量选择器
 */
public class QuantitySelectorView extends RelativeLayout implements Button.OnClickListener {
    protected Button addButton, substractButton, quanitityButton;
    private int MAX_COUNT  = 9999;
    private QuantityChangeListener listener;

    public QuantitySelectorView(Context context) {
        super(context);
        init();
    }

    public QuantitySelectorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public QuantitySelectorView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public int getMAX_COUNT() {
        return MAX_COUNT;
    }

    public void setMAX_COUNT(int MAX_COUNT) {
        this.MAX_COUNT = MAX_COUNT;
    }

    public Button getQuanitityButton() {
        return quanitityButton;
    }

    protected void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View v = inflater.inflate(R.layout.common_quantity_selecotor_view, this);


        addButton = (Button) v.findViewById(R.id.quantityview_add);
        addButton.setOnClickListener(this);

        quanitityButton = (Button) v.findViewById(R.id.quantityview_count);
        quanitityButton.setOnClickListener(this);

        substractButton = (Button) v.findViewById(R.id.quantityview_substract);
        substractButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        boolean isQuanitityButton = false;
        int count = 1;
        try {
            count= Integer.parseInt(quanitityButton.getText()+"");
        } catch (NumberFormatException e) {
            count = 1;
        }

        if (getMAX_COUNT() < 1) {
            count = 0;
        }

        switch (v.getId()) {
            case R.id.quantityview_add:
                if (count < MAX_COUNT) {
                    count ++;
                    addButton.setEnabled(true);
                    substractButton.setEnabled(true);
                } else {
                    addButton.setEnabled(false);
                    Toast.makeText(getContext(), "库存不足！！", Toast.LENGTH_LONG).show();
                }
                Log.d("QuantitySelectorView", "add");
                break;
            case R.id.quantityview_substract:
                if (count > 1) {
                    count--;
                    substractButton.setEnabled(true);
                    addButton.setEnabled(true);
                } else {
                    substractButton.setEnabled(false);
                }
                Log.d("QuantitySelectorView", "sub");
                break;
            default:
                isQuanitityButton = true;
                Log.d("QuantitySelectorView", "default");
        }

        quanitityButton.setText(count+"");

        if (listener != null) {
            Log.d("count", "count:" + count);
            listener.onChanged(count, isQuanitityButton);
        }

        quanitityButton.setText("" + count);
    }


    /**
     * 获取当前数量
     * @return
     */
    public int getCount() {
        int count = 0;
        try {
            if (quanitityButton != null) {
                count = Integer.parseInt(quanitityButton.getText().toString());
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return count;
    }

    public interface QuantityChangeListener  {
        void onChanged(int num, boolean isQuanitityButton);
    }

    public void setQuantityChangeListener(QuantityChangeListener quantityChangeListener) {
        this.listener = quantityChangeListener;
    }
}
